/**
 * 添加或者修改页面
 */
var BtyZxBaoxiaodanInfoDlg = {
    data: {
        id: "",
        shopName: "",
        userName: "",
        userId: "",
        productRemark: "",
        applyDate: "",
        auditTime: "",
        reason: "",
        status: "",
        fileUrl: "",
        bxdaId: "",
        createTime: "",
        createUser: "",
        updateTime: "",
        updateUser: "",
        reimbursementAmount: "",
        remitAmount: ""
    }
};

layui.use(['form', 'admin', 'ax','laydate','upload','formSelects'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var upload = layui.upload;

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/btyZxBaoxiaodan/remarkHistory");
    var result = ajax.start();
    $('#productRemark').val(result.data.productRemark);
    if (result.data.productRemark) {
        $('#productRemark').attr("readonly", "readonly");
    }


    //执行实例
    var uploadInst = upload.render({
        elem: '#upload' //绑定元素
        ,url: '/btyZxBaoxiaodan/upload' //上传接口
        ,accept: 'file' //允许上传的文件类型
        ,done: function(res){
          //上传完毕回调
          $('#fileUrl').val(res.data.src);
          Feng.success("货品单导入成功！");
        }
        ,error: function(){
          //请求异常回调
        }
    });

    // 初始门店
    function initShop(){
        $("#shopName").empty();
        $("#shopName").append("<option value=''>请选择门店</option>");
        var languageAjax = new $ax(Feng.ctxPath + "/mdzx/ghsList");
        var languageResult = languageAjax.start();
        $.each(languageResult.data, function (index, val) {
            if(val.btyBrandIdView!=undefined){
                //查询用的门店
                $("#shopName").append("<option value='" + val.btyBrandIdView + "-" + val.btyZxMdzxId + "'>" + val.btyBrandIdView + "</option>");
            }
        });
        form.render('select');
    }
    initShop();

    // 表单提交事件 提交后 变为工程审核中状态 status = 2
    form.on('submit(btnSubmit)', function (data) {
        if (data.field.fileUrl == '') {
            Feng.error("请先上传货品单");
            return;
        }
        admin.btnLoading('#btnSubmit', "提交中");
        var ajax = new $ax(Feng.ctxPath + "/btyZxBaoxiaodan/addItem", function (data) {
            if (data.success) {
                Feng.success("提交成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            } else {
                admin.btnLoading('#btnSubmit', false);
                Feng.error(data.message);
            }

        }, function (data) {
            admin.btnLoading('#btnSubmit', false);
            Feng.error("提交失败！" + data.responseJSON.message)
        });
        data.field.status = 2;
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    // 表单保存事件 保存变为待提交状态 status = 1
    form.on('submit(btnSave)', function (data) {
        if (data.field.fileUrl == '') {
            Feng.error("请先上传货品单");
            return;
        }
        admin.btnLoading('#btnSave', "保存中");
        var ajax = new $ax(Feng.ctxPath + "/btyZxBaoxiaodan/addItem", function (data) {
            if (data.success) {
                Feng.success("保存成功！");
                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);
                //关掉对话框
                admin.closeThisDialog();
            } else {
                admin.btnLoading('#btnSave', false);
                Feng.error(data.message)
            }

        }, function (data) {
            admin.btnLoading('#btnSave', false);
            Feng.error("保存失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();

        return false;
    });

    $('#cancel').click(function(){
        window.location.href = Feng.ctxPath + '/btyZxBaoxiaodan'
    });

});